package com.ktg.mes.rec.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.mes.rec.mapper.RecGrindRecordMapper;
import com.ktg.mes.rec.domain.RecGrindRecord;
import com.ktg.mes.rec.service.IRecGrindRecordService;



/**
 * 研磨记录Service业务层处理
 *
 * @author byteco
 * @date 2022-10-31
 */
@Service
public class RecGrindRecordServiceImpl extends ServiceImpl<RecGrindRecordMapper, RecGrindRecord> implements IRecGrindRecordService
{
    @Autowired
    private RecGrindRecordMapper recGrindRecordMapper;



    /**
     * 查询研磨记录
     *
     * @param entity 研磨记录
     * @param page 分页对象
     * @return 研磨记录
     */
    @Override
    public IPage<RecGrindRecord> queryPageList(RecGrindRecord entity, Page<RecGrindRecord> page) {
        LambdaQueryWrapper<RecGrindRecord> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(RecGrindRecord::getCreateTime);
        IPage<RecGrindRecord> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询研磨记录
     *
     * @param entity 研磨记录
     * @return 研磨记录
     */
    @Override
    public List<RecGrindRecord> queryList(RecGrindRecord entity) {
        LambdaQueryWrapper<RecGrindRecord> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(RecGrindRecord::getCreateTime);
        return this.list(lqw);
    }

    public RecGrindRecord getInfo(Long id)
    {
        QueryWrapper<RecGrindRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RecGrindRecord::getGrindId, id);
        return this.getOne(queryWrapper);
    }


    /**
     * 新增研磨记录
     *
     * @param entity 研磨记录
     * @return 结果
     */
    @Override
    public void insert(RecGrindRecord entity)
    {
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(new Date());
        this.save(entity);
    }

    /**
     * 修改研磨记录
     *
     * @param entity 研磨记录
     * @return 结果
     */
    @Override
    public boolean update(RecGrindRecord entity)
    {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(new Date());
        return this.updateById(entity);
    }

    /**
     * 批量删除研磨记录
     *
     * @param grindIds 需要删除的研磨记录主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] grindIds)
    {

        return this.removeByIds(Arrays.asList(grindIds));
    }

    /**
     * 删除研磨记录信息
     *
     * @param grindId 研磨记录主键
     * @return 结果
     */
    @Override
    public boolean delete(Long grindId)
    {
        return this.removeById(grindId);
    }

    private LambdaQueryWrapper<RecGrindRecord> buildQueryWrapper(RecGrindRecord entity) {
        LambdaQueryWrapper<RecGrindRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(entity.getWorkshop()), RecGrindRecord::getWorkshop, entity.getWorkshop());
        lqw.like(StringUtils.isNotBlank(entity.getMachineName()), RecGrindRecord::getMachineName, entity.getMachineName());
        lqw.like(StringUtils.isNotBlank(entity.getProcessName()), RecGrindRecord::getProcessName, entity.getProcessName());
        lqw.eq(StringUtils.isNotBlank(entity.getToolTypeCode()), RecGrindRecord::getToolTypeCode, entity.getToolTypeCode());
        lqw.like(StringUtils.isNotBlank(entity.getToolTypeName()), RecGrindRecord::getToolTypeName, entity.getToolTypeName());
        lqw.eq(StringUtils.isNotBlank(entity.getSpecification()), RecGrindRecord::getSpecification, entity.getSpecification());
        lqw.like(StringUtils.isNotBlank(entity.getTaskName()), RecGrindRecord::getTaskName, entity.getTaskName());
        lqw.eq(entity.getStartTime() != null, RecGrindRecord::getStartTime, entity.getStartTime());
        lqw.eq(StringUtils.isNotBlank(entity.getGrindDuration()), RecGrindRecord::getGrindDuration, entity.getGrindDuration());
        lqw.eq(entity.getDrainageStartTime() != null, RecGrindRecord::getDrainageStartTime, entity.getDrainageStartTime());
        lqw.eq(StringUtils.isNotBlank(entity.getDrainageDuration()), RecGrindRecord::getDrainageDuration, entity.getDrainageDuration());
        lqw.eq(entity.getIsFilter() != null, RecGrindRecord::getIsFilter, entity.getIsFilter());
        lqw.eq(StringUtils.isNotBlank(entity.getRemarks()), RecGrindRecord::getRemarks, entity.getRemarks());
        return lqw;
    }
}
